package nl.hu.verbrandendephoenix.view;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.shared.Auction;
import nl.hu.verbrandendephoenix.shared.Bid;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SearchAuction {

	private Panel pnlSearchAuction, pnlResult;
	private Grid grdSearchAuctionForm, grdResult, grdPrice;
	private ListBox lboCategory;
	private Label lblAuctionName, lblPrice, lblCategory;
	private TextBox txtAuctionName, txtPriceMin, txtPriceMax;
	private Button btnSearch, btnBack;
	
	public SearchAuction() {
		pnlSearchAuction = new VerticalPanel();
		pnlSearchAuction.addStyleName("post");
		pnlResult = new VerticalPanel();
	}

	public Panel constructForm() {
		grdSearchAuctionForm = new Grid(5, 3);
		
		new Label();

		Label title = new Label("Veiling zoeken:");
		title.addStyleName("h1");
		pnlSearchAuction.add(title);

		lblCategory = new Label("Categorie");

		grdSearchAuctionForm.setWidget(0, 0, lblCategory);

		lblAuctionName = new Label("Productnaam: ");
		txtAuctionName = new TextBox();

		grdSearchAuctionForm.setWidget(1, 0, lblAuctionName);
		grdSearchAuctionForm.setWidget(1, 1, txtAuctionName);

		grdPrice = new Grid(1, 5);
		lblPrice = new Label("Prijs tussen:");
		txtPriceMin = new TextBox();
		txtPriceMin.setWidth("50px");
		grdPrice.setWidget(0, 0, txtPriceMin);
		grdPrice.setWidget(0, 1, new Label("credits"));
		grdPrice.setWidget(0, 2, new Label("en"));
		txtPriceMax = new TextBox();
		txtPriceMax.setWidth("50px");
		grdPrice.setWidget(0, 3, txtPriceMax);
		grdPrice.setWidget(0, 4, new Label("credits"));

		grdSearchAuctionForm.setWidget(2, 0, lblPrice);
		grdSearchAuctionForm.setWidget(2, 1, grdPrice);

		btnSearch = new Button("Zoeken");

		grdSearchAuctionForm.setWidget(3, 1, btnSearch);

		pnlSearchAuction.add(grdSearchAuctionForm);

		return pnlSearchAuction;
	}

	public Panel constructResultForm(Grid result) {
		pnlResult.add(result);
		
		btnBack = new Button("Terug");
		pnlResult.add(btnBack);
		
		return pnlResult;
	}

	public Grid printResult(ArrayList<Auction> result, ArrayList<Bid> result2) {
		grdResult = new Grid(result.size() + 1, 6);
		grdResult.setWidget(0, 0, new HTML("<b>Veilingnaam</b>"));
		grdResult.setWidget(0, 1, new HTML("<b>Beginprijs</b>"));
		grdResult.setWidget(0, 2, new HTML("<b>Huidige bod</b>"));
		grdResult.setWidget(0, 3, new HTML("<b>Begindatum</b>"));
		grdResult.setWidget(0, 4, new HTML("<b>Categorie</b>"));
		int rowCounter = 1;
		for(final Auction a : result) {			
			grdResult.setWidget(rowCounter, 0, new Anchor(a.getAuctionName(), "#viewauction-" + a.getAuctionID()));
			grdResult.setWidget(rowCounter, 1, new Label((int)a.getIniPrice() + " credits"));
			grdResult.setWidget(rowCounter, 2, new Label("geen"));
			for(Bid b : result2) {
				if(b.getAuctionID() == a.getAuctionID())
					grdResult.setWidget(rowCounter, 2, new Label((int)b.getPrice() + " credits"));
			}
			grdResult.setWidget(rowCounter, 3, new Label(a.getEndDate().toString()));
			grdResult.setWidget(rowCounter, 4, new Label(a.getCategory()));
			rowCounter++;
		}
		return grdResult;
	}

	public ArrayList<TextBox> getArrTextBox() {
		ArrayList<TextBox> arrTextBox = new ArrayList<TextBox>();
		arrTextBox.add(txtAuctionName);
		arrTextBox.add(txtPriceMin);
		arrTextBox.add(txtPriceMax);
		return arrTextBox;
	}

	public Button getBtnSearch() {
		return btnSearch;
	}

	public Button getBtnBack() {
		return btnBack;
	}

	public TextBox getTxtProductName() {
		return txtAuctionName;
	}

	public TextBox getTxtPriceMin() {
		return txtPriceMin;
	}

	public TextBox getTxtPriceMax() {
		return txtPriceMax;
	}

	public ListBox getLboCategory() {
		return lboCategory;
	}

	public String getLboCategorySelection() {
		return lboCategory.getValue(lboCategory.getSelectedIndex());
	}

	public void setLboCategory(String[] result) {
		lboCategory = new ListBox();
		lboCategory.addItem("Alles");
		for(int i = 0; i < result.length; i++) {
			lboCategory.addItem(result[i]);
		}
		grdSearchAuctionForm.setWidget(0, 1, lboCategory);
	}
}